import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/login/login.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(VueRouter)
const routes = [{
        path: '/',
        name: 'login',
        meta: {
            title: '登录'
        },
        component: Login
    }, {
        path: '/home',
        name: 'home',
        meta: {
            title: '后台首页'
        },
        redirect: 'home/calendar',
        children: [{
            path: 'calendar',
            name: 'calendar',
            meta: {
                title: '日历'
            },
            component: () =>
                import ('../views/Home/calendar.vue')
        }, {
            path: 'wz',
            name: 'wz',
            meta: {
                title: '位置'
            },
            component: () =>
                import ('../views/login/dw.vue')
        }],
        component: () =>
            import ('../views/Home/index.vue')
    },

]

//动态路由
const ruleMapping = {
    'userlist': {
        path: 'userlist',
        name: 'userlist',
        meta: {
            title: '用户列表'
        },
        component: () =>
            import ('../views/Home/user/user.vue')
    },
    'agereport': {
        path: 'agereport',
        name: 'agereport',
        meta: {
            title: '年龄数据可视化'
        },
        component: () =>
            import ('../views/Home/report/agereport.vue')
    },
    'sexreport': {
        path: 'sexreport',
        name: 'sexreport',
        meta: {
            title: '性别数据可视化'
        },
        component: () =>
            import ('../views/Home/report/sexreport.vue')
    },
    'ProductList': {
        path: 'ProductList',
        name: 'ProductList',
        meta: {
            title: '商品列表'
        },
        component: () =>
            import ('../views/Home/shop/ProductList.vue')
    },
    'ProductPut': {
        path: 'ProductPut',
        name: 'ProductPut',
        meta: {
            title: '商品上架'
        },
        component: () =>
            import ('../views/Home/shop/ProductPut.vue')
    },
    'ProductClass': {
        path: 'ProductClass',
        name: 'ProductClass',
        meta: {
            title: '分类列表'
        },
        component: () =>
            import ('../views/Home/shop/ProductClass.vue')
    },
    'MyPut': {
        path: 'MyPut',
        name: 'MyPut',
        meta: {
            title: '我的上架'
        },
        component: () =>
            import ('../views/Home/shop/MyPut.vue')
    },
    'Treviewed': {
        path: 'Treviewed',
        name: 'Treviewed',
        meta: {
            title: '待审核商品'
        },
        component: () =>
            import ('../views/Home/shop/T.vue')
    },
    'Myreviewed': {
        path: 'Myreviewed',
        name: 'Myreviewed',
        meta: {
            title: '我的审核'
        },
        component: () =>
            import ('../views/Home/shop/mT.vue')
    }
}
const router = new VueRouter({
    routes,

})
router.beforeEach((to, from, next) => {

    document.title = to.meta.title;
    NProgress.start()
    next()
})

router.afterEach(() => {
    NProgress.done()
})
export function initDynamicRoutes() {
    const currentRoutes = router.options.routes;
    let paths = JSON.parse(sessionStorage.getItem('rightlistpath'));

    paths.forEach(element => {
        element.children.forEach(element => {
            const temp = ruleMapping[element.path]
            temp.meta.kk = element.rights
            currentRoutes[1].children.push(temp)
        });
    });

    router.addRoutes(currentRoutes)
}
export default router